<?php
require_once( 'DBConnection.php');

class GearDao
{
	const TBL_GEAR = 'gear';
	
	private $tablename = self::TBL_GEAR;
	private $dbconn = null;
	public function __construct()
	{
		$this->dbconn = DBConnection::getConnection();
	}
	
	public function getGearFields()
	{
	
		$gear_table = self::TBL_GEAR;
		$sql = "DESC $gear_table";
		
		$this->dbconn->setupQuery( $sql);
		$result = $this->dbconn->executeQuery( );
		
		$out = array();
		foreach ($result as $row)
		{
			$out[] = $row['Field'];
		}
		
		return $out;
	}	//end of getGearFields
	
	public function getGearList( $options = array() )
	{
		$q1 =<<<SQL
SELECT * FROM  `gear` g1		
SQL;
		
		$q2 =<<<SQL
INNER JOIN (
	SELECT g.id, c.name classname, gt.name gearname
		FROM  `gear` g
	  LEFT OUTER JOIN class c ON ( g.class__ = c.lookup ) 
	  LEFT OUTER JOIN geartype gt ON ( g.type__ = gt.lookup )
)g2 ON ( g2.id = g1.id ) 
SQL;
		
		$append = '';
		foreach ($options as $key => $val)
		{
			$append .= " `$key` = '$val'";
			$append .= " AND ";
		}
		
		if ($append !== '')
		{				//chop off ' AND ' from the end of the string.
			$q1 .= ' WHERE ' . substr( $append, 0, strlen($append) -5);
		}
		$sql = $q1 . $q2;
		echo $sql;
		$this->dbconn->setupQuery( $sql);
		$result = $this->dbconn->executeQuery();
		
		return $result;
	}
	
}	//end of class